package com.fitness.club.util;

import com.google.common.base.CaseFormat;

import java.io.File;

public class DataStringUtil {

    private static String[] fieldPrefix = new String[]{"bs_", "os_", "sys_", "rs_", "uuid_", "vc_", "int_", "dt_",
            "fl_", "dl_", "ft_", "jm", "out_", "qg_"};

    public static String getEntityFileName(String str) {
        return DataStringUtil.getEntityFileName(null, str);
    }

    public static String getEntityFileName(String[] fieldPrefix, String str) {
        if (fieldPrefix != null && fieldPrefix.length > 0) {
            DataStringUtil.fieldPrefix = fieldPrefix;
        }
        for (String prefix : DataStringUtil.fieldPrefix) {
            if (str.startsWith(prefix)) {
                str = str.replaceAll(prefix, "");
                break;
            }
        }
        return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, str);
    }

    public static String getColumnType(String dbColumnType) {
        if (dbColumnType.equalsIgnoreCase("char") || dbColumnType.equalsIgnoreCase("varchar")
                || dbColumnType.equalsIgnoreCase("text") || dbColumnType.equalsIgnoreCase("nchar")
                || dbColumnType.equalsIgnoreCase("nvarchar") || dbColumnType.equalsIgnoreCase("longtext")) {
            return "String";
        } else if (dbColumnType.equalsIgnoreCase("int") || dbColumnType.equalsIgnoreCase("integer")
                || dbColumnType.equalsIgnoreCase("bit") || dbColumnType.equalsIgnoreCase("bigint") || dbColumnType.equalsIgnoreCase("tinyint")) {
            return "Integer";
        } else if (dbColumnType.equalsIgnoreCase("float")) {
            return "Float";
        } else if (dbColumnType.equalsIgnoreCase("double")) {
            return "Double";
        } else if (dbColumnType.equalsIgnoreCase("decimal") || dbColumnType.equalsIgnoreCase("numeric")) {
            return "Double";
        } else if (dbColumnType.equalsIgnoreCase("date") || dbColumnType.equalsIgnoreCase("datetime") || dbColumnType.equalsIgnoreCase("datetime2")
                || dbColumnType.equalsIgnoreCase("time") || dbColumnType.equalsIgnoreCase("timestamp")
        ) {
            return "Date";
        } else {
            return "String";
        }
    }


    public static String getEntityPath(String tableName, String filePath) {
        StringBuffer str = new StringBuffer(System.getProperty("user.dir")).append(File.separator).append(filePath).append(File.separator).append("entity").append(File.separator);
        str.append(CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, tableName)).append(".java");
        return str.toString();
    }

    public static String getMapperPath(String tableName, String filePath) {
        StringBuffer str = new StringBuffer(System.getProperty("user.dir")).append(File.separator).append(filePath).append(File.separator).append("mapper").append(File.separator);
        str.append(CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, tableName)).append("Mapper.java");
        return str.toString();
    }

    public static String getProviderPath(String tableName, String filePath) {
        StringBuffer str = new StringBuffer(System.getProperty("user.dir")).append(File.separator).append(filePath).append(File.separator).append("provider").append(File.separator);
        str.append(CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, tableName)).append("SqlProvider.java");
        return str.toString();
    }

    public static String getQueryPath(String tableName, String filePath) {
        StringBuffer str = new StringBuffer(System.getProperty("user.dir")).append(File.separator).append(filePath).append(File.separator).append("entity").append(File.separator).append("query").append(File.separator);
        str.append(CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, tableName)).append("Query.java");
        return str.toString();
    }
}
